import { type DataTableColumns, NButton, NTag } from 'naive-ui'
import type { CreateColumsOptions } from '@/types/fcform'
import { cloneDeep } from 'lodash-es'
import { usePageConfig } from '../index.ts'
import { h } from 'vue'

export const createColums = (options: CreateColumsOptions): DataTableColumns => {
  const { action, handDel } = options
  const pageConfig = usePageConfig()
  return [
    {
      title: '菜单名称',
      key: 'meta.title',
      width: 260,
    },
    {
      title: '页面路径',
      key: 'component',
    },
    {
      title: '路由',
      key: 'path',
      width: 200,
    },
    {
      title: '类型',
      key: 'menuType',
      width: 80,
      render(row) {
        return h(
          NTag,
          {
            type: row.menuType === '0' ? 'success' : 'default',
          },
          { default: () => (row.menuType === '0' ? '菜单' : '按钮') },
        )
      },
    },
    {
      title: '权限标识',
      key: 'permission',
      width: 200,
      render(row) {
        return h('div', {}, { default: () => row.permission || '--' })
      },
    },
    {
      title: '排序',
      key: 'sortOrder',
      align: 'center',
      width: 80,
    },
    {
      title: '操作',
      key: 'total',
      align: 'center',
      width: 220,
      render(row) {
        const arr = []
        const addBtn = h(
          NButton,
          {
            size: 'small',
            type: 'primary',
            quaternary: true,
            onClick: () =>
              action.default({
                ...pageConfig.edit.default.defaultValue,
                parentId: row.id,
                component: '',
              }),
          },
          { default: () => '添加' },
        )
        const editBtn = h(
          NButton,
          {
            size: 'small',
            type: 'warning',
            quaternary: true,
            onClick: () => {
              const params = cloneDeep(row)
              params.title = params?.meta?.title
              params.enName = params?.meta?.enName
              params.icon = row?.meta?.icon
              params.showNext = params?.meta?.showNext
              params.keepAlive = params?.meta?.isKeepAlive
              action.default(params)
            },
          },
          { default: () => '编辑' },
        )
        const delBtn = h(
          NButton,
          {
            size: 'small',
            quaternary: true,
            type: 'error',
            onClick: () => handDel(row),
          },
          { default: () => '删除' },
        )
        if (commonUnit.auth('sys_menu_add')) {
          arr.push(addBtn)
        }
        if (commonUnit.auth('sys_menu_edit')) {
          arr.push(editBtn)
        }
        if (commonUnit.auth('sys_menu_del')) {
          arr.push(delBtn)
        }
        return h('div', arr)
      },
    },
  ]
}
